package com.spittr.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.spittr.db.SpittleRepository;
import com.spittr.domain.Spitter;
import com.spittr.domain.Spittle;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 吐槽控制器类
 * 
 * @author wben
 * @version v1.0
 */
@Controller
@RequestMapping("/spittles")
@SessionAttributes({ "currentPage" })
public class SpittleController {

	@Autowired
	private SpittleRepository spittleRepository;

	/**
	 * 最新吐槽
	 * @param count
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public List<Spittle> spittles(@RequestParam(value = "count", defaultValue = "20") int count) {
		return spittleRepository.findRecent(count);
	}

	/**
	 * 查看单个吐槽
	 * @param spittleId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{spittleId}", method = RequestMethod.GET)
	public String spittle(@PathVariable("spittleId") long spittleId, Model model) {
		model.addAttribute(spittleRepository.findOne(spittleId));
		return "spittle";
	}

	/**
	 * 新建一个吐槽
	 * @param request
	 * @param form
	 * @param model
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String saveSpittle(HttpServletRequest request, SpittleForm form, Model model, HttpSession session)
			throws Exception {
		spittleRepository
				.save(new Spittle(null, (Spitter) session.getAttribute("spitter"), form.getMessage(), new Date(),
						false, null, null));
		return "redirect:/spittles";
	}

	/**
	 * 分页查询吐槽
	 *
	 * @param currentPage
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/spittleList",method = GET)
	public String getAllSpittlesByPages(@RequestParam(value = "currentPage", defaultValue = "1")int currentPage, Model model) {
		// 上一页：判断page是否小于1
		boolean noLess = currentPage <= 1;
		if (noLess) currentPage = 1;
		List<Spittle> spittleList = spittleRepository.selectByPage((currentPage - 1) * 5, 5);
		long size = spittleRepository.count();
		// 下一页：判断page是否超限
		boolean noMore = (size - currentPage * 5L) <= 0;
		model.addAttribute("spittleList", spittleList);
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("noLess", noLess);
		model.addAttribute("noMore", noMore);
		return "spittleList";
	}

	/**
	 * 通过 id审核吐槽
	 *
	 * @param spittleId
	 * @return
	 */
	@RequestMapping(value = "/checkSpittle", method = POST)
	public String checkSpittleById(@RequestParam(value = "spittleId")int spittleId) {
		spittleRepository.checkById(spittleId);
		return "redirect:/spittles/spittleList";
	}

	/**
	 * 通过 id删除吐槽
	 *
	 * @param spittleId
	 * @return
	 */
	@RequestMapping(value = "/deleteSpittle", method = POST)
	public String deleteSpittleById(@RequestParam(value = "spittleId")int spittleId) {
		spittleRepository.delete(spittleId);
		return "redirect:/spittles/spittleList";
	}
}
